package com.ujoin.service;

import org.springframework.beans.factory.annotation.Autowired;

import com.ujoin.bean.Activity;
import com.ujoin.bean.ActivityMessage;
import com.ujoin.bean.User;
import com.ujoin.dao.IUserDAO;
import com.ujoin.dao.imp.ActivityDAO;
import com.ujoin.dao.imp.ActivityMessageDAO;
import com.ujoin.util.JsonUtil;

public class ActivityMessageService {
	@Autowired
	private ActivityMessageDAO activityMessDao;
	@Autowired
	private IUserDAO userDao;
	@Autowired
	private ActivityDAO activityDao;
	
	public String createActivityMessage(ActivityMessage activityMess){
		Activity a = activityDao.findById(activityMess.getActivityId());
		User u = (User) userDao.findByEmail(activityMess.getReceiverId());
		if(a == null){
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("û�����������ʵ��");
			return jo.toJSONString();
		}
		else if(u == null){
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("û������˺ţ����ʵ��");
			return jo.toJSONString();
		}
		else{
			activityMessDao.save(activityMess);
			JsonUtil jo = JsonUtil.setAllComponentsName(activityMessDao
					.findById(activityMess.getActivityMessId())).setSuccess();
			return jo.toJSONString();
		}
	}
	
	public String deleteActivityMessage(ActivityMessage activityMess){
		Activity a = activityDao.findById(activityMess.getActivityId());
		User u = (User) userDao.findByEmail(activityMess.getReceiverId());
		if(a == null){
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("û�����������ʵ��");
			return jo.toJSONString();
		}
		else if(u == null){
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("û������˺ţ����ʵ��");
			return jo.toJSONString();
		}
		else{
			activityMessDao.delete(activityMess);
			JsonUtil jo = JsonUtil.getSuccess();
			return jo.toJSONString();
		}
	}
}
